﻿using DevExpress.XtraGrid.Views.Grid;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_SpecialitySetForm_List : BaseForm
    {
        ServiceBase service = new ServiceBase();
        public Base_SpecialitySetForm_List()
        {
            InitializeComponent();
            BindInfo();
        }

        private void groupControl7_DoubleClick(object sender, EventArgs e)
        {
            Base_SpecialitySetForm_SumScore_Option from = new Base_SpecialitySetForm_SumScore_Option();
            from.ShowDialog();
        }
        void BindInfo()
        {
            BindSpecialityKindsInfo();
            BindDic_ExamLevel();
        }

        #region 绑定专业
        void BindSpecialityKindsInfo()
        {
            DataTable dt = service.GetSpecialityKindsTable("", "", "", "");
            dataGridViewX1.DataSource = dt;
        }
        #endregion
        #region 绑定级别
        void BindDic_ExamLevel()
        {
            try
            {
                lst_ExamLevel.DataSource = null;
                DataTable dt = service.GetSpecialityLevelMapping(GetSpecialityKindsNo());
                lst_ExamLevel.DisplayMember = "ExamLevelName";
                lst_ExamLevel.ValueMember = "ExamLevelNO";
                lst_ExamLevel.DataSource = dt;
            }
            catch
            { }

        }
        #endregion
        #region 绑定考试方式
        void BindDic_ExamType()
        {
            string strExamLevel = string.Empty;
            if (lst_ExamLevel.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)lst_ExamLevel.SelectedItem;
                strExamLevel = drExamPlace.Row["ExamLevelNO"].ToSafeString();
            }
            lst_ExamType.DataSource = null;
            using (DataTable dt = service.GetSpecialityLevelExamTypeMappingNameTable(GetSpecialityKindsNo(), strExamLevel, SessionerHelper.SystemYear))
            {
                if (dt != null)
                {
                    dt.Columns.Add("ExamTypeName_New");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i]["ExamTypeName_New"] = dt.Rows[i]["ExamTypeName"].ToSafeString() + "-" + dt.Rows[i]["ExamScoreTypeName"].ToSafeString() + "-" + dt.Rows[i]["CalculateScoreName"].ToSafeString();
                    }
                    lst_ExamType.DataSource = dt;
                    lst_ExamType.DisplayMember = "ExamTypeName_New";
                    lst_ExamType.ValueMember = "SpecialityLevelMappingID";
                }
            }
        }
        #endregion

        private string GetExamLevelNo()
        {
            string ExamLevel = string.Empty;
            if (lst_ExamLevel.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)lst_ExamLevel.SelectedItem;
                ExamLevel = drExamPlace.Row["ExamLevelNO"].ToSafeString();
            }
            return ExamLevel;
        }

        private string GetSpecialityKindsNo()
        {
            string strSpecialityKindsNo_Detail = string.Empty;
            GridView rowsSelected = gridView1 as GridView;
            if (rowsSelected.Rows.Count > 0)
            {
                int[] inteurm = rowsSelected.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    strSpecialityKindsNo_Detail = rowsSelected.Rows[inteurm[0]].DataRow.ItemArray[0].ToString();
                }
            }
            return strSpecialityKindsNo_Detail;
        }

        private void btnAdd_KSFS_Click(object sender, EventArgs e)
        {

            string strSpecialityKindsNO = string.Empty;
            strSpecialityKindsNO = GetSpecialityKindsNo();
            if (strSpecialityKindsNO.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择专业!");
                return;
            }
            string strExamLevel = string.Empty;
            string strExamLevelName = string.Empty;
            if (lst_ExamLevel.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)lst_ExamLevel.SelectedItem;
                strExamLevel = drExamPlace.Row["ExamLevelNO"].ToSafeString();
                strExamLevelName = drExamPlace.Row["ExamLevelName"].ToSafeString();
            }
            if (strExamLevel.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择考试级别!");
                return;
            }
            string strExamTypeNo = string.Empty;
            Base_ExamTypeForm option = new Base_ExamTypeForm(strSpecialityKindsNO, strExamLevel, strExamTypeNo, strExamLevelName);
            if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindDic_ExamType();
            }
        }

        private void btnAddExamLevel_Click(object sender, EventArgs e)
        {
            string strSpecialityKindsNO = string.Empty;
            strSpecialityKindsNO = GetSpecialityKindsNo();
            //if (strSpecialityKindsNO.Length == 0)
            //{
            //    DevHelper.ShowInfoMessageBox("请先选择专业!");
            //    return;
            //}

            Base_ExamLevel_Select option = new Base_ExamLevel_Select(strSpecialityKindsNO);
            if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.DialogResult = DialogResult.OK;
                BindDic_ExamLevel();
            }
        }

        private void btnDeleteLevel_Click(object sender, EventArgs e)
        {
            if (lst_ExamLevel.SelectedIndex < 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择要删除的项!");
                return;
            }
            string ExamLevel = string.Empty;
            if (lst_ExamLevel.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)lst_ExamLevel.SelectedItem;
                ExamLevel = drExamPlace.Row["ExamLevelNO"].ToSafeString();
            }
            service.DeleteSpecialityLevelExamTypeMapping("", GetSpecialityKindsNo(), ExamLevel);
            BindDic_ExamLevel();
        }

        private void gridView1_RowClick(object sender, RowClickEventArgs e)
        {
            BindDic_ExamLevel();
        }

        private void lst_ExamLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindDic_ExamType();
        }

        private void btnDeleteExamType_Click(object sender, EventArgs e)
        {
            string strExamType = string.Empty;
            if (lst_ExamType.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)lst_ExamType.SelectedItem;
                strExamType = drExamPlace.Row.ItemArray[0].ToSafeString();
            }
            if (strExamType.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请先选择考试级别!");
                return;
            }

            string strExamLevel = string.Empty;
            string strExamLevelName = string.Empty;
            if (lst_ExamLevel.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)lst_ExamLevel.SelectedItem;
                strExamLevel = drExamPlace.Row["ExamLevelNO"].ToSafeString();
                strExamLevelName = drExamPlace.Row["ExamLevelName"].ToSafeString();
            }
            service.DeleteSpecialityLevelExamTypeMapping(strExamType, GetSpecialityKindsNo(), strExamLevel);
            BindDic_ExamType();
        }

        private void lst_ExamType_DoubleClick(object sender, EventArgs e)
        {
            string strExamType = string.Empty;
            if (lst_ExamType.SelectedItem != null)
            {
                DataRowView drExamPlace = (DataRowView)lst_ExamType.SelectedItem;
                strExamType = drExamPlace.Row.ItemArray[0].ToSafeString();
            }
            //if (strExamType.Length == 0)
            //{
            //    DevHelper.ShowInfoMessageBox("请先考试级别!");
            //    return;
            //}
            Base_ExamTypeForm option = new Base_ExamTypeForm(strExamType);
            if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BindDic_ExamType();
            }
        }
    }
}
